<script setup>
import { ref, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import NewShopAuthService from '../services/NewShopAuthService.js'

const router = useRouter()
const username = ref('')
const userEmail = ref('')
const products = ref([
  {
    id: 1,
    name: '高级无线耳机',
    price: 899,
    image: 'https://via.placeholder.com/300x300?text=Wireless+Headphones',
    description: '高品质无线耳机，支持主动降噪，续航长达24小时',
    category: '数码配件'
  },
  {
    id: 2,
    name: '智能手表 Pro',
    price: 1999,
    image: 'https://via.placeholder.com/300x300?text=Smart+Watch+Pro',
    description: '全面健康监测，支持多种运动模式，50米防水',
    category: '智能穿戴'
  },
  {
    id: 3,
    name: '超薄笔记本电脑',
    price: 6999,
    image: 'https://via.placeholder.com/300x300?text=Thin+Laptop',
    description: '高性能轻薄本，16GB内存，512GB固态硬盘，长续航',
    category: '电脑办公'
  },
  {
    id: 4,
    name: '智能扫地机器人',
    price: 2499,
    image: 'https://via.placeholder.com/300x300?text=Robot+Vacuum',
    description: '激光导航，智能避障，自动充电，支持APP控制',
    category: '智能家居'
  }
])

// 商品分类数据
const categories = ref([
  {
    id: 1,
    name: '数码配件',
    subcategories: [
      { id: 101, name: '耳机耳麦' },
      { id: 102, name: '移动电源' },
      { id: 103, name: '数据线' },
      { id: 104, name: '充电器' },
      { id: 105, name: '保护膜/贴膜' }
    ]
  },
  {
    id: 2,
    name: '智能穿戴',
    subcategories: [
      { id: 201, name: '智能手表' },
      { id: 202, name: '智能手环' },
      { id: 203, name: '智能眼镜' },
      { id: 204, name: '健康监测' }
    ]
  },
  {
    id: 3,
    name: '电脑办公',
    subcategories: [
      { id: 301, name: '笔记本电脑' },
      { id: 302, name: '平板电脑' },
      { id: 303, name: '台式机' },
      { id: 304, name: '显示器' },
      { id: 305, name: '办公设备' }
    ]
  },
  {
    id: 4,
    name: '智能家居',
    subcategories: [
      { id: 401, name: '智能音箱' },
      { id: 402, name: '智能照明' },
      { id: 403, name: '扫地机器人' },
      { id: 404, name: '智能门锁' },
      { id: 405, name: '安防监控' }
    ]
  },
  {
    id: 5,
    name: '手机通讯',
    subcategories: [
      { id: 501, name: '智能手机' },
      { id: 502, name: '手机配件' },
      { id: 503, name: '通讯设备' }
    ]
  },
  {
    id: 6,
    name: '家用电器',
    subcategories: [
      { id: 601, name: '空调' },
      { id: 602, name: '冰箱' },
      { id: 603, name: '洗衣机' },
      { id: 604, name: '电视' },
      { id: 605, name: '厨房电器' }
    ]
  }
])

const selectedCategory = ref(null)
const isCategoryDropdownOpen = ref(null)

    id: 1,
    name: '高级无线耳机',
    price: 899,
    image: 'https://via.placeholder.com/300x300?text=Wireless+Headphones',
    description: '高品质无线耳机，支持主动降噪，续航长达24小时'
  },
  {
    id: 2,
    name: '智能手表 Pro',
    price: 1999,
    image: 'https://via.placeholder.com/300x300?text=Smart+Watch+Pro',
    description: '全面健康监测，支持多种运动模式，50米防水'
  },
  {
    id: 3,
    name: '超薄笔记本电脑',
    price: 6999,
    image: 'https://via.placeholder.com/300x300?text=Thin+Laptop',
    description: '高性能轻薄本，16GB内存，512GB固态硬盘，长续航'
  },
  {
    id: 4,
    name: '智能扫地机器人',
    price: 2499,
    image: 'https://via.placeholder.com/300x300?text=Robot+Vacuum',
    description: '激光导航，智能避障，自动充电，支持APP控制'
  }
])

// 检查登录状态
onMounted(() => {
  checkLoginStatus()
})

// 检查登录状态的函数
const checkLoginStatus = () => {
  if (!NewShopAuthService.isLoggedIn()) {
    // 如果未登录，跳转到登录页面
    router.push('/new-shop-login')
    return
  }
  
  // 获取当前登录用户信息
  const user = NewShopAuthService.getCurrentUser()
  if (user) {
    username.value = user.username
    userEmail.value = user.email
  }
}

// 处理退出登录
const handleLogout = () => {
  NewShopAuthService.logout()
  router.push('/new-shop-login')
}

// 查看商品详情
const viewProductDetail = (productId) => {
  router.push(`/new-shop/product/${productId}`)
}

// 查看商品列表
const viewProductList = (categoryId, categoryName) => {
  router.push({ 
    path: '/new-shop/products',
    query: { category: categoryId, categoryName: categoryName }
  })
}

// 打开分类下拉菜单
const openCategoryDropdown = (categoryId) => {
  isCategoryDropdownOpen.value = isCategoryDropdownOpen.value === categoryId ? null : categoryId
}

// 关闭分类下拉菜单
const closeCategoryDropdown = () => {
  isCategoryDropdownOpen.value = null
}
</script>

<template>
  <div class="new-shop-home-container">
    <!-- 导航栏 -->
    <nav class="new-shop-nav">
      <div class="nav-logo">
        <svg viewBox="0 0 24 24" width="24" height="24" stroke="#2196F3" stroke-width="2" fill="none" stroke-linecap="round" stroke-linejoin="round">
          <circle cx="9" cy="21" r="1"></circle>
          <circle cx="20" cy="21" r="1"></circle>
          <path d="M1 1h4l2.68 13.39a2 2 0 0 0 2 1.61h9.72a2 2 0 0 0 2-1.61L23 6H6"></path>
        </svg>
        <span>NewShop</span>
      </div>
      
      <div class="nav-search">
        <input type="text" placeholder="搜索商品..." class="search-input" />
        <button class="search-button">搜索</button>
      </div>
      
      <div class="nav-actions">
        <button class="nav-button" @click="router.push('/new-shop/cart')">
          <svg viewBox="0 0 24 24" width="20" height="20" stroke="#666" stroke-width="2" fill="none" stroke-linecap="round" stroke-linejoin="round">
            <circle cx="9" cy="21" r="1"></circle>
            <circle cx="20" cy="21" r="1"></circle>
            <path d="M1 1h4l2.68 13.39a2 2 0 0 0 2 1.61h9.72a2 2 0 0 0 2-1.61L23 6H6"></path>
          </svg>
          <span>购物车</span>
        </button>
        
        <div class="user-menu">
          <button class="user-menu-button">
            <svg viewBox="0 0 24 24" width="20" height="20" stroke="#666" stroke-width="2" fill="none" stroke-linecap="round" stroke-linejoin="round">
              <path d="M20 21v-2a4 4 0 0 0-4-4H8a4 4 0 0 0-4 4v2"></path>
              <circle cx="12" cy="7" r="4"></circle>
            </svg>
            <span>{{ username }}</span>
          </button>
          <div class="user-menu-dropdown">
            <div class="user-info">
              <p class="user-name">{{ username }}</p>
              <p class="user-email">{{ userEmail }}</p>
            </div>
            <hr />
            <button class="menu-item">个人中心</button>
            <button class="menu-item">我的订单</button>
            <button class="menu-item">收藏夹</button>
            <hr />
            <button class="menu-item logout" @click="handleLogout">退出登录</button>
          </div>
        </div>
      </div>
    </nav>
    
    <!-- 分类导航 -->
    <div class="category-nav" @mouseleave="closeCategoryDropdown">
      <div class="category-container">
        <div 
          v-for="category in categories" 
          :key="category.id"
          class="category-item"
          @mouseenter="openCategoryDropdown(category.id)"
        >
          <button class="category-button">
            {{ category.name }}
            <svg viewBox="0 0 24 24" width="16" height="16" stroke="#666" stroke-width="2" fill="none" stroke-linecap="round" stroke-linejoin="round">
              <polyline points="6 9 12 15 18 9"></polyline>
            </svg>
          </button>
          
          <!-- 子分类下拉菜单 -->
          <div 
            v-if="isCategoryDropdownOpen === category.id"
            class="subcategory-dropdown"
          >
            <div class="subcategory-content">
              <button 
                v-for="subcategory in category.subcategories" 
                :key="subcategory.id"
                class="subcategory-item"
                @click="viewProductList(subcategory.id, subcategory.name)"
              >
                {{ subcategory.name }}
              </button>
            </div>
          </div>
        </div>
      </div>
    </div>
    
    <!-- 主要内容 -->
    <main class="new-shop-main">
      <!-- 英雄区域 -->
      <section class="hero-section">
        <div class="hero-content">
          <h1>欢迎来到 NewShop</h1>
          <p>发现最新科技产品，享受便捷购物体验</p>
          <div class="hero-buttons">
            <button class="primary-button">立即探索</button>
            <button class="secondary-button">查看优惠</button>
          </div>
        </div>
      </section>
      
      <!-- 商品列表 -->
      <section class="products-section">
        <div class="section-header">
          <h2>热门商品</h2>
          <button class="view-more-button">查看更多</button>
        </div>
        
        <div class="products-grid">
          <div class="product-card" v-for="product in products" :key="product.id" @click="viewProductDetail(product.id)">
            <div class="product-image">
              <img :src="product.image" :alt="product.name" />
            </div>
            <div class="product-info">
              <h3 class="product-name">{{ product.name }}</h3>
              <p class="product-description">{{ product.description }}</p>
              <div class="product-price">¥{{ product.price }}</div>
              <button class="add-to-cart-button">加入购物车</button>
            </div>
          </div>
        </div>
      </section>
    </main>
    
    <!-- 页脚 -->
    <footer class="new-shop-footer">
      <div class="footer-content">
        <div class="footer-column">
          <h3>关于我们</h3>
          <ul>
            <li>公司简介</li>
            <li>联系我们</li>
            <li>招贤纳士</li>
            <li>隐私政策</li>
          </ul>
        </div>
        <div class="footer-column">
          <h3>客户服务</h3>
          <ul>
            <li>帮助中心</li>
            <li>退换货政策</li>
            <li>配送信息</li>
            <li>支付方式</li>
          </ul>
        </div>
        <div class="footer-column">
          <h3>商务合作</h3>
          <ul>
            <li>商家入驻</li>
            <li>广告合作</li>
            <li>营销中心</li>
          </ul>
        </div>
        <div class="footer-column">
          <h3>关注我们</h3>
          <div class="social-links">
            <a href="#">微信</a>
            <a href="#">微博</a>
            <a href="#">抖音</a>
          </div>
        </div>
      </div>
      <div class="footer-bottom">
        <p>© 2024 NewShop. 保留所有权利。</p>
      </div>
    </footer>
  </div>
</template>

<style scoped>
.new-shop-home-container {
  display: flex;
  flex-direction: column;
  min-height: 100vh;
}

/* 导航栏样式 */
.new-shop-nav {
  background-color: white;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
  padding: 15px 30px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  position: sticky;
  top: 0;
  z-index: 1000;
}

/* 分类导航样式 */
.category-nav {
  background-color: white;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
  border-top: 1px solid #f0f0f0;
  position: sticky;
  top: 70px;
  z-index: 999;
}

.category-container {
  display: flex;
  max-width: 1200px;
  margin: 0 auto;
}

.category-item {
  position: relative;
}

.category-button {
  display: flex;
  align-items: center;
  gap: 5px;
  padding: 15px 25px;
  background: none;
  border: none;
  cursor: pointer;
  font-size: 16px;
  color: #333;
  transition: all 0.3s ease;
}

.category-button:hover {
  color: #2196F3;
  background-color: #f5f5f5;
}

.subcategory-dropdown {
  position: absolute;
  top: 100%;
  left: 0;
  right: 0;
  background-color: white;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
  border-radius: 0 0 8px 8px;
  z-index: 1000;
}

.subcategory-content {
  padding: 20px;
}

.subcategory-item {
  display: block;
  width: 100%;
  text-align: left;
  padding: 10px 20px;
  background: none;
  border: none;
  cursor: pointer;
  font-size: 14px;
  color: #333;
  transition: all 0.3s ease;
  border-radius: 6px;
}

.subcategory-item:hover {
  background-color: #f5f5f5;
  color: #2196F3;
}

.nav-logo {
  display: flex;
  align-items: center;
  font-size: 24px;
  font-weight: 700;
  color: #2196F3;
}

.nav-logo svg {
  margin-right: 10px;
}

.nav-search {
  display: flex;
  align-items: center;
  max-width: 500px;
  width: 100%;
  margin: 0 40px;
}

.search-input {
  flex: 1;
  padding: 10px 15px;
  border: 1px solid #ddd;
  border-radius: 6px 0 0 6px;
  font-size: 16px;
}

.search-button {
  background-color: #2196F3;
  color: white;
  border: none;
  padding: 10px 20px;
  border-radius: 0 6px 6px 0;
  cursor: pointer;
  font-size: 16px;
}

.nav-actions {
  display: flex;
  align-items: center;
}

.nav-button {
  display: flex;
  align-items: center;
  margin-right: 20px;
  background: none;
  border: none;
  cursor: pointer;
  font-size: 16px;
  color: #666;
}

.nav-button svg {
  margin-right: 5px;
}

.user-menu {
  position: relative;
}

.user-menu-button {
  display: flex;
  align-items: center;
  background: none;
  border: none;
  cursor: pointer;
  font-size: 16px;
  color: #666;
}

.user-menu-button svg {
  margin-right: 5px;
}

.user-menu-dropdown {
  position: absolute;
  top: 100%;
  right: 0;
  background-color: white;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
  border-radius: 8px;
  min-width: 200px;
  display: none;
}

.user-menu:hover .user-menu-dropdown {
  display: block;
}

.user-info {
  padding: 15px;
  border-bottom: 1px solid #eee;
}

.user-name {
  font-weight: 600;
  margin: 0 0 5px 0;
}

.user-email {
  font-size: 14px;
  color: #666;
  margin: 0;
}

.menu-item {
  width: 100%;
  text-align: left;
  padding: 10px 15px;
  background: none;
  border: none;
  cursor: pointer;
  font-size: 14px;
}

.menu-item:hover {
  background-color: #f5f5f5;
}

.menu-item.logout {
  color: #f44336;
}

.menu-item.logout:hover {
  background-color: #ffebee;
}

/* 主要内容样式 */
.new-shop-main {
  flex: 1;
}

.hero-section {
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  padding: 100px 30px;
  text-align: center;
  color: white;
}

.hero-content h1 {
  font-size: 48px;
  margin: 0 0 20px 0;
  font-weight: 700;
}

.hero-content p {
  font-size: 20px;
  margin: 0 0 30px 0;
}

.hero-buttons {
  display: flex;
  justify-content: center;
  gap: 15px;
}

.primary-button {
  background-color: white;
  color: #2196F3;
  border: none;
  padding: 12px 30px;
  border-radius: 6px;
  font-size: 16px;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.3s ease;
}

.primary-button:hover {
  background-color: #f5f5f5;
  transform: translateY(-1px);
}

.secondary-button {
  background-color: transparent;
  color: white;
  border: 2px solid white;
  padding: 12px 30px;
  border-radius: 6px;
  font-size: 16px;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.3s ease;
}

.secondary-button:hover {
  background-color: rgba(255, 255, 255, 0.1);
  transform: translateY(-1px);
}

.products-section {
  padding: 60px 30px;
  background-color: #fafafa;
}

.section-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 40px;
}

.section-header h2 {
  font-size: 32px;
  margin: 0;
  color: #333;
}

.view-more-button {
  background: none;
  border: none;
  color: #2196F3;
  font-size: 16px;
  font-weight: 600;
  cursor: pointer;
}

.products-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(280px, 1fr));
  gap: 30px;
  max-width: 1200px;
  margin: 0 auto;
}

.product-card {
  background-color: white;
  border-radius: 12px;
  overflow: hidden;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
  cursor: pointer;
  transition: all 0.3s ease;
}

.product-card:hover {
  transform: translateY(-5px);
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.15);
}

.product-image {
  width: 100%;
  height: 200px;
  overflow: hidden;
}

.product-image img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  transition: transform 0.3s ease;
}

.product-card:hover .product-image img {
  transform: scale(1.05);
}

.product-info {
  padding: 20px;
}

.product-name {
  font-size: 20px;
  margin: 0 0 10px 0;
  color: #333;
}

.product-description {
  font-size: 14px;
  color: #666;
  margin: 0 0 15px 0;
  line-height: 1.5;
}

.product-price {
  font-size: 24px;
  font-weight: 700;
  color: #2196F3;
  margin: 0 0 15px 0;
}

.add-to-cart-button {
  width: 100%;
  background-color: #2196F3;
  color: white;
  border: none;
  padding: 10px;
  border-radius: 6px;
  font-size: 16px;
  cursor: pointer;
  transition: all 0.3s ease;
}

.add-to-cart-button:hover {
  background-color: #1976D2;
}

/* 页脚样式 */
.new-shop-footer {
  background-color: #333;
  color: white;
  padding: 60px 30px 30px 30px;
}

.footer-content {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
  gap: 40px;
  max-width: 1200px;
  margin: 0 auto;
  margin-bottom: 40px;
}

.footer-column h3 {
  font-size: 18px;
  margin: 0 0 20px 0;
  font-weight: 600;
}

.footer-column ul {
  list-style: none;
  padding: 0;
  margin: 0;
}

.footer-column li {
  margin-bottom: 10px;
  cursor: pointer;
  transition: color 0.3s ease;
}

.footer-column li:hover {
  color: #2196F3;
}

.social-links {
  display: flex;
  gap: 20px;
}

.social-links a {
  color: white;
  text-decoration: none;
  transition: color 0.3s ease;
}

.social-links a:hover {
  color: #2196F3;
}

.footer-bottom {
  text-align: center;
  padding-top: 20px;
  border-top: 1px solid #444;
  font-size: 14px;
  color: #999;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .category-container {
    overflow-x: auto;
    white-space: nowrap;
  }
  
  .category-item {
    flex-shrink: 0;
  }
  
  .subcategory-dropdown {
    position: fixed;
    left: 0;
    right: 0;
    top: auto;
    bottom: 0;
    max-height: 60vh;
    overflow-y: auto;
  }
}
</style>